/*
*
*  ExileServer_PlayerMarket_network_createNewListingRequest.sqf
*  Author: WolfkillArcadia
*  © 2016 Arcas Industries
*  This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.
*  To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/.
*/
private["_sessionID","_package","_itemArray","_itemClassname","_price","_location","_newListing","_playerObject","_playerUID","_listingID","_vehicle","_la","_vehicleObjectNetID","_vehicleObject","_availableHitpoints","_vehicleHitpoints"];
_sessionID = _this select 0;
_package = _this select 1;
_itemArray = _package select 0;
_itemClassname = _itemArray select 0;
_price = parseNumber(_itemArray select 1);
_location = _itemArray select 2;

_newListing = [];
try
{
  _playerObject = _sessionID call ExileServer_system_session_getPlayerObject;
  _playerUID = getPlayerUID _playerObject;
  if (isNull _playerObject) then
	{
		throw "服务器无法搜寻到您的ID";
	};
  if !(alive _playerObject) then
	{
		throw "您已死亡";
	};
  if (_itemClassname isEqualTo "") then
  {
    throw "物品名称不存在";
  };
  if (_price <= 0) then
  {
    throw "销售价格不能小于或等于0";
  };
  _listingID = call AfterlifeServer_market_createListingID;
  if (_listingID isEqualTo "") then
  {
    throw "物品ID创建失败";
  };
  _vehicle = false;
  _la = [_itemClassname];

  _newListing =
  [
    _listingID,
    1,
    _la,
    str(_price),
    _playerUID
  ];

  AfterlifeListing pushBack _newListing;

  AfterlifeListing = [AfterlifeListing, [], {(_x select 2) select 0}, "DESCEND"] call BIS_fnc_sortBy;

  ["updateListingResponse",[AfterlifeListing]] call AfterlifeServer_system_network_send_broadcast;

  [_sessionID,"listItemResponse",[_vehicle,_itemClassname,str(_price),_location]] call AfterlifeServer_system_network_send_to;

  format ["createNewListing:%1:%2:%3:%4:%5",_listingID,1,_la,_price,_playerUID] call ExileServer_system_database_query_fireAndForget;

  format ["玩家%1以$ %3价格上市了物品%2 - 新物品数值: %4",name _playerObject,_itemClassname, _price,_newListing] call AfterlifeServer_util_log;
}
catch
{
    [_sessionID, "toastRequest", ["ErrorTitleAndText", ["Whoops!", _exception]]] call ExileServer_system_network_send_to;
    [_exception] call AfterlifeServer_util_log;
};
